#!/usr/bin/env python3
"""
初始化首屏内容
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app.db.database import SessionLocal
from app.models.homepage import Homepage
from app.models.user import User

def init_homepage():
    db = SessionLocal()
    try:
        # 检查是否已有首屏内容
        existing_homepage = db.query(Homepage).count()
        if existing_homepage > 0:
            print("✅ 首屏内容已存在，跳过初始化")
            return
        
        # 获取管理员用户
        admin_user = db.query(User).filter(User.username == "admin").first()
        if not admin_user:
            print("❌ 请先运行 init_data.py 创建管理员用户")
            return
        
        # 创建首屏内容
        homepage_data = {
            "title": "欢迎来到商品展示系统",
            "content": """
            <div class="text-center">
                <h1 class="text-4xl font-bold text-gray-900 mb-6">欢迎来到商品展示系统</h1>
                <p class="text-xl text-gray-600 mb-8">
                    这是一个现代化的商品管理系统，提供完整的商品展示和管理功能。
                </p>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <h3 class="text-lg font-semibold text-blue-900 mb-2">商品管理</h3>
                        <p class="text-blue-700">完整的商品CRUD操作，支持分类、标签、图片管理</p>
                    </div>
                    <div class="bg-green-50 p-6 rounded-lg">
                        <h3 class="text-lg font-semibold text-green-900 mb-2">首屏管理</h3>
                        <p class="text-green-700">富文本编辑器，版本控制，一键发布</p>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <h3 class="text-lg font-semibold text-purple-900 mb-2">用户界面</h3>
                        <p class="text-purple-700">响应式设计，现代化UI，优秀用户体验</p>
                    </div>
                </div>
                <div class="bg-gray-100 p-6 rounded-lg">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">技术栈</h3>
                    <div class="flex flex-wrap justify-center gap-4 text-sm">
                        <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded">FastAPI</span>
                        <span class="bg-green-100 text-green-800 px-3 py-1 rounded">Next.js</span>
                        <span class="bg-yellow-100 text-yellow-800 px-3 py-1 rounded">TypeScript</span>
                        <span class="bg-purple-100 text-purple-800 px-3 py-1 rounded">TailwindCSS</span>
                        <span class="bg-red-100 text-red-800 px-3 py-1 rounded">MySQL</span>
                    </div>
                </div>
            </div>
            """,
            "version": 1,
            "is_published": True,
            "is_active": True,
            "created_by": admin_user.id,
        }
        
        homepage = Homepage(**homepage_data)
        db.add(homepage)
        db.commit()
        print("✅ 首屏内容初始化完成!")
        
    except Exception as e:
        print(f"❌ 初始化失败: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    init_homepage() 